<template>
  <el-dialog
    v-el-drag-dialog
    :title="title"
    :visible.sync="visible"
    width="500px"
    @close="reset"
  >
    <el-form ref="form" :rules="rules" class="form" :model="query" label-width="110px">
      <el-form-item label="门店：" prop="orgSNNo">
        <el-tree-select
          v-model="query.orgSNNo"
          :disabled="type==='edit'"
          :clearable="true"
          class="tree-select"
          :popover-width="360"
          :select-params="{
            clearable: true,
            placeholder: '请选择组织机构',
            loading: $loadingPlugin.getTreeData
          }"
          :tree-params=" {
            'default-expand-all': true,
            'expand-on-click-node': false,
            'highlight-current': true,
            props:{
              value:'orgSNNo',
              label: 'orgName',
              disabled: 'disabled',
            },
            data: treeData
          }"
          @node-click="selectTreeChange"
        />
      </el-form-item>
      <el-form-item label="期初日期：" class="merge-item" prop="startDt">
        <el-date-picker
          v-model="query.startDt"
          :disabled="type==='edit'"
          style="width: 100%;"
          clearable
          type="date"
          placeholder="选择日期"
        />
      </el-form-item>
      <el-form-item label="现金期初数：" class="merge-item" prop="cashAmount">
        <el-input v-model="query.cashAmount" placeholder="请输入" clearable />
      </el-form-item>
      <el-form-item label="月结期初数：" class="merge-item" prop="monthlyAmount">
        <el-input v-model.trim="query.monthlyAmount" placeholder="请输入" clearable />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="reset">
        关闭
      </el-button>
      <el-button :loading="$loadingPlugin.success " type="primary" @click="success">
        确认
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { initialaccountSave } from '@/api'
  export default {
    name: 'add',
    components: {

    },
    props: {
      treeData: {
        type: Array,
        default: []
      },
      orgList: {
        type: Array,
        default: []
      }
    },
    data() {
      return {
        visible: false,
        title: null,
        type: 'add',
        rules: {
          orgSNNo: [{ required: true, message: '请选择', trigger: 'blur' }],
          startDt: [{ required: true, message: '请选择', trigger: 'blur' }],
          monthlyAmount: [{ required: true, message: '请输入', trigger: 'blur' }],
          cashAmount: [{ required: true, message: '请输入', trigger: 'blur' }]
        },
        orgObj: {},
        query: {
          cashAmount: null,
          monthlyAmount: null,
          orgSNNo: null,
          startDt: null,
          isUpdate: 0
        }
      }
    },
    methods: {
      open(param) {
        this.visible = true
        this.type = 'add'
        this.title = '新增资金期初设置'
        if (param) {
          this.type = 'edit'
          this.title = `修改资金期初设置`
          console.log({ ...param })
          this.query = { ...this.query, ...param, isUpdate: 1 }
        }
      },
      async success() {
        try {
          var flag = await this.$refs.form.validate()
          if (flag) {
            if (this.query.startDt * 1 > new Date() * 1) {
              this.$message.error('期初时间不能大于当前系统时间')
              return false
            }
            const params = {
              ...this.query
            }
            var orgObj = this.orgList.find(l => this.query.orgSNNo === l.orgSNNo)
            params['orgName'] = orgObj.orgName
            params['orgId'] = orgObj.orgId
            const res = await initialaccountSave(params)
            if (res.success) {
              this.reset()
              this.$emit('refresh')
            }
          }
        } catch (e) {
          console.log(e)
        }
      },
      selectTreeChange(val) {
        // this.orgObj = val
      },
      reset() {
        this.query = {
          cashAmount: null,
          monthlyAmount: null,
          orgSNNo: null,
          startDt: null,
          isUpdate: 0
        }
        this.visible = false
      }
    }

  }
</script>

<style lang="scss" scoped>

</style>

